package com.jackrain.nea.fc.ps.service;


import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.parser.Feature;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.util.TypeUtils;
import com.jackrain.nea.exception.NDSException;
import com.jackrain.nea.fc.ps.api.OmsProDistribQueryCmd;
import com.jackrain.nea.fc.ps.mapper.OmsProDistribMapper;
import com.jackrain.nea.sys.CommandAdapter;
import com.jackrain.nea.util.ApplicationContextHandle;
import com.jackrain.nea.util.ValueHolder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;


/**
 * 配销中心商品档案 全量查询 发送至oms
 *
 * @author kangccong
 * @create 2018/08/21
 */
@Slf4j
@Component
@Service(protocol = "dubbo", validation = "true", version = "1.0", group = "ps")
public class OmsProDistribQueryCmdImpl extends CommandAdapter implements OmsProDistribQueryCmd {

    @Override
    public ValueHolder execute(HashMap map) throws NDSException {

//        log.debug("-----------配销中心商品档案查询全量---start----");

        ValueHolder vh = new ValueHolder();

        OmsProDistribMapper omsProDistribMapper = ApplicationContextHandle.getBean(OmsProDistribMapper.class);
        JSONArray jsonArray = new JSONArray();

        //获取并处理商品档案数据
        List<HashMap> listData = omsProDistribMapper.selectProdistrib();

        for (HashMap hashMap : listData) {
//            hashMap.put("GOODSTYPEID", "1");
//            hashMap.put("OWNERID", "FCAPI");
//            hashMap.put("CREATIONDATE", new Timestamp(System.currentTimeMillis()).toString());
//            hashMap.put("MODIFIERID", "FCAPI");
//            hashMap.put("MODIFIEDDATE", new Timestamp(System.currentTimeMillis()).toString());
//            hashMap.put("ISACTIVE", "Y");
            //品牌名称
            String brandName = omsProDistribMapper.selectBrandName(TypeUtils.castToLong(hashMap.get("PS_C_BRAND_ID")));
            hashMap.put("BRANDNAME", brandName);

            //获取 处理条码数据
            JSONArray itemArray = new JSONArray();
            List<HashMap> skuData = omsProDistribMapper.selectSkudistrib(TypeUtils.castToLong(hashMap.get("ID")));

            for (HashMap item : skuData) {
                Long spec1objid = TypeUtils.castToLong(item.get("PS_C_SPEC1OBJ_ID"));
                Long spec2objid = TypeUtils.castToLong(item.get("PS_C_SPEC2OBJ_ID"));

                JSONObject jsonmap1 = new JSONObject();
                JSONObject jsonmap2 = new JSONObject();

                if (spec1objid != null) {
                    HashMap map1 = omsProDistribMapper.selectPsCSpecobj(spec1objid);
                    jsonmap1 = new JSONObject(map1);
                }
                if (spec2objid != null) {
                    HashMap map2 = omsProDistribMapper.selectPsCSpecobj(spec2objid);
                    jsonmap2 = new JSONObject(map2);
                }

                String ename1 = jsonmap1.getString("ENAME");
                String ecode1 = jsonmap1.getString("ECODE");
                String ename2 = jsonmap2.getString("ENAME");
                String ecode2 = jsonmap2.getString("ECODE");

                item.put("PS_C_PRO_ECODE", hashMap.get("ECODE"));
                item.put("CLR_ECODE", ecode1);
                item.put("CLR_ENAME", ename1);
                item.put("SIZE_ECODE", ecode2);
                item.put("SIZE_ENAME", ename2);
                item.put("ECODE", item.get("ECODE"));
                item.put("GBCODE", item.get("GBCODE"));
                item.put("PS_C_SPEC_ECODE", ecode1 + ecode2);
                item.put("PS_C_SPEC_ENAME", ename1 + ename2);
                item.put("PRICELIST", hashMap.get("PRICELIST"));

                item.remove("PS_C_SPEC1OBJ_ID");
                item.remove("PS_C_SPEC2OBJ_ID");
                JSONObject itemjs = new JSONObject(item);
                itemArray.add(itemjs);
            }

            hashMap.remove("ID");
            hashMap.remove("PS_C_BRAND_ID");

            JSONObject js = new JSONObject(hashMap);
            js.put("PS_C_SKU", itemArray);
            JSONObject param1 = JSON.parseObject(JSON.toJSONStringWithDateFormat(js,
                    "yyyy-MM-dd HH:mm:ss", SerializerFeature.WriteMapNullValue), Feature.OrderedField);
            jsonArray.add(param1);
        }

//        log.debug("-----------配销中心商品档案查询全量---end----"+jsonArray);
        log.debug("OmsProDistribQueryCmdImpl--jsonArray-->{}",jsonArray);

        vh.put("data", jsonArray);
        vh.put("code", 0);
        vh.put("message", "查询成功！");
        return vh;
    }


}
